<?php

class MStorageRecord extends MY_Model
{
    const ORDER_ID_DESC = 1;
    const ORDER_ID_ASC = 2;

    const STATUS_DISABLED = 0;
    const STATUS_SUCCESS = 1;
    const STATUS_ONLOAD = 2;
    protected $table = 'storage_record';

    function __construct()
    {
        parent::__construct();
    }

    public static function getOrderType($orderType = NULL)
    {
        $define = array(
            self::ORDER_ID_DESC => 'ID倒序',
            self::ORDER_ID_ASC => 'ID升序'
        );
        if ($orderType === NULL) {
            return $define;
        } else {
            return key_exists($orderType, $define) ? $define[$orderType] : '';
        }
    }

    public static function getState($state = NULL)
    {
        $define = array(self::STATUS_DISABLED => '<label class="label label-danger">作废</label>', self::STATUS_SUCCESS => '<label class="label label-success">成功</label>',self::STATUS_ONLOAD => '<label class="label label-warning">在途</label>');
        if ($state === NULL) {
            return $define;
        } else {
            return key_exists($state, $define) ? $define[$state] : '';
        }
    }

    public static function getOutState($state = NULL)
    {
        $define = array(self::STATUS_DISABLED => '<label class="label label-danger">作废</label>', self::STATUS_SUCCESS => '<label class="label label-success">成功</label>');
        if ($state === NULL) {
            return $define;
        } else {
            return key_exists($state, $define) ? $define[$state] : '';
        }
    }

    public function getList($limit, $sort = false)
    {
        $this->db->select('r.*,p.product_name,p.price,w.location,w.area');
        $this->db->distinct();
        $this->db->from($this->table.' r');
        $this->db->join('storage_product p','r.product_id = p.id','LEFT');
        $this->db->join('storage_warehouse w','r.product_id = w.product_id and r.warehousing = w.warehousing','LEFT');

        $this->db->limit($limit['limit'], $limit['offset']);
        $this->formfilter->doFilter();

        if ($sort) {
            switch ($sort) {
                case self::ORDER_ID_DESC:
                    $this->db->order_by('id', 'desc');
                    break;
                case self::ORDER_ID_ASC:
                    $this->db->order_by('id', 'asc');
                    break;
            }
        } else {
            $this->db->order_by('id', 'desc');
        }

        $query = $this->db->get();
        return $query->result();
    }

    public function getDeliverList($limit, $sort = false){
        $this->db->select('r.*,p.product_name,p.price,w.location,w.area');
        $this->db->distinct();
        $this->db->from($this->table.' r');
        $this->db->join('storage_product p','r.product_id = p.id','LEFT');
        $this->db->join('storage_warehouse w','r.product_id = w.product_id and r.warehousing = w.warehousing','LEFT');
        $this->db->where('r.type',0);
        $this->db->where('r.has_deliver',0);
        $this->db->where('r.status <>',0);
        $this->db->where('r.type_mark in (3,4,5,6,7,8)');

        $this->db->limit($limit['limit'], $limit['offset']);
        $this->formfilter->doFilter();

        if ($sort) {
            switch ($sort) {
                case self::ORDER_ID_DESC:
                    $this->db->order_by('id', 'desc');
                    break;
                case self::ORDER_ID_ASC:
                    $this->db->order_by('id', 'asc');
                    break;
            }
        } else {
            $this->db->order_by('id', 'desc');
        }

        $query = $this->db->get();
        return $query->result();
    }

    public function getDeliverCount(){
        $this->db->select('count(distinct r.id) as num');
        $this->db->distinct();
        $this->db->from($this->table.' as r');
        $this->db->where('r.type',0);
        $this->db->where('r.has_deliver',0);
        $this->db->join('storage_product p','r.product_id = p.id','LEFT');
        $this->db->join('storage_warehouse w','r.product_id = w.product_id and r.warehousing = w.warehousing','LEFT');
        $this->formfilter->doFilter();

        $tmp = $this->db->get()->result();
        return (int)$tmp[0]->num;
    }


    public function getCount()
    {
        $this->db->select('count(distinct r.id) as num');
        $this->db->distinct();
        $this->db->from($this->table.' as r');
        $this->db->join('storage_product p','r.product_id = p.id','LEFT');
        $this->db->join('storage_warehouse w','r.product_id = w.product_id and r.warehousing = w.warehousing','LEFT');
        $this->formfilter->doFilter();

        $tmp = $this->db->get()->result();
        return (int)$tmp[0]->num;
    }

    public function getById($id)
    {
        $this->db->select('r.*,w.location,w.area,p.product_name');
        $this->db->distinct();
        $this->db->from($this->table.' r');
        $this->db->join('storage_warehouse w','r.product_id = w.product_id and r.warehousing = w.warehousing','LEFT');
        $this->db->join('storage_product p','r.product_id = p.id','LEFT');
        $this->db->where('r.id',$id);
        return $this->db->get()->row();
    }

    public function getByIds($ids_str){
        $this->db->select('r.*,p.product_name,p.price,w.location');
        $this->db->distinct();
        $this->db->from($this->table.' as r');
        $this->db->join('storage_product p','r.product_id = p.id');
        $this->db->join('storage_warehouse w','r.warehousing = w.warehousing');
        $this->db->where('r.id in ('.$ids_str.')');
        $query = $this->db->get();
        return $query->result();
    }

    public function getRecordByTime($product_id,$warehousing){
        $this->db->select('*');
        $this->db->distinct();
        $this->db->from($this->table);
        $this->db->where('product_id',$product_id);
        $this->db->where('warehousing',$warehousing);

        $query = $this->db->get();
        return $query->row();
    }

    public function updateRecordDeliver($ids_str,$shipping_no){
        $ids = explode (',',$ids_str);
        $updateInfo = array();
        foreach ($ids as $k => $v){
            $updateInfo[$k]['id'] = $v;
            $updateInfo[$k]['has_deliver'] = 1;
            $updateInfo[$k]['shipping_no'] = $shipping_no;
        }
        $this->db->update_batch($this->table,$updateInfo,'id');
    }

    public function updateRecordUnDeliver($ids_str){
        $ids = explode (',',$ids_str);
        $updateInfo = array();
        foreach ($ids as $k => $v){
            $updateInfo[$k]['id'] = $v;
            $updateInfo[$k]['has_deliver'] = 0;
        }
        $this->db->update_batch($this->table,$updateInfo,'id');
    }

    public function getAllRecord($ids_str){
        $ids = '('.$ids_str.')';
        $this->db->select('r.*,p.product_name,p.price,w.location,w.area');
        $this->db->distinct();
        $this->db->from($this->table.' r');
        $this->db->join('storage_product p','r.product_id = p.id','LEFT');
        $this->db->join('storage_warehouse w','r.product_id = w.product_id and r.warehousing = w.warehousing','LEFT');
        $this->db->where('r.id in '.$ids);

        $query = $this->db->get();
        return $query->result();
    }

    public function getAjaxDetail($id){
        $this->db->select('r.*,p.product_name,p.price,w.location');
        $this->db->distinct();
        $this->db->from($this->table.' as r');
        $this->db->join('storage_product p','r.product_id = p.id');
        $this->db->join('storage_warehouse w','r.warehousing = w.warehousing');
        $this->db->where('r.id',$id);
        $query = $this->db->get();
        return $query->result();
    }

}
